{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 730,
   "id": "d78910dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = 'E:\\\\code\\\\notebookPrjs\\\\nowcoder-interview-process-master\\\\data'\n",
    "input_file = data_dir + '\\\\input.txt'\n",
    "input_bak_file = data_dir + '\\\\input_bak.txt'\n",
    "output_left_file = data_dir + '\\\\output_left.txt'\n",
    "output_match_file = data_dir + '\\\\output_match.txt'\n",
    "output_flt_file = data_dir + '\\\\output_filter.txt'\n",
    "not_fit_urls_file = data_dir + '\\\\not_fit_urls.txt'\n",
    "output_fit_file = data_dir + '\\\\output_fit.txt'\n",
    "key_words = ['KMP', '逻辑题', '下棋', '跳台阶', '智力题', '相交', '之和', '多边形', '斜率', '概率', '糖果', '青蛙', '窗口数', '算式', '前五']\n",
    "use_re_match = False\n",
    "#use_re_match = True\n",
    "#update_input_file = False\n",
    "update_input_file = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 731,
   "id": "8cc96442",
   "metadata": {},
   "outputs": [],
   "source": [
    "if update_input_file:\n",
    "    print('update input file')\n",
    "    # backup input file\n",
    "    with open(input_file, 'r', encoding='utf-8') as source_file:\n",
    "        content = source_file.read()\n",
    "    with open(input_bak_file, 'w', encoding='utf-8') as target_file:\n",
    "        target_file.write(content)\n",
    "    \n",
    "    # update input file    \n",
    "    with open(output_left_file, 'r', encoding='utf-8') as source_file:\n",
    "        content = source_file.read()\n",
    "    with open(input_file, 'w', encoding='utf-8') as target_file:\n",
    "        target_file.write(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 732,
   "id": "518df481",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Record:\n",
    "    def __init__(self, text, link):\n",
    "        self.text_ = text\n",
    "        self.link_ = link\n",
    "        \n",
    "    def text(self):\n",
    "        return self.text_\n",
    "    \n",
    "    def link(self):\n",
    "        return self.link_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 733,
   "id": "950e3de8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total records count is 117\n"
     ]
    }
   ],
   "source": [
    "recs = []\n",
    "row_index = 0\n",
    "text = ''\n",
    "link = ''\n",
    "with open(input_file, 'r', encoding='utf-8') as file:\n",
    "    # 使用for循环逐行读取文件内容\n",
    "    for line in file:\n",
    "        # 打印每一行的内容。注意，这里每行末尾可能会有换行符'\\n'，\n",
    "        # 如果不希望在打印结果中保留换行符，可以使用rstrip()方法去除。\n",
    "        # print(row_index)\n",
    "        ri = row_index % 3\n",
    "        if ri == 0:\n",
    "            text = line.rstrip('\\n')\n",
    "        elif ri == 1:\n",
    "            if line.strip() != '链接':\n",
    "                print(row_index)\n",
    "                print(line)\n",
    "                break\n",
    "        if ri == 2:\n",
    "            link = line.rstrip('\\n')\n",
    "            rec = Record(text, link)\n",
    "            recs.append(rec)\n",
    "        #   print(len(recs))\n",
    "        # print(line.rstrip('\\n'))\n",
    "        row_index += 1\n",
    "\n",
    "print('Total records count is ' + str(len(recs)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 734,
   "id": "ba64dfb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# unfit_urls = []\n",
    "# with open(not_fit_urls_file, 'r', encoding='utf-8') as file:\n",
    "#     for line in file:\n",
    "#         unfit_urls.append(line.rstrip())\n",
    "# print('Total unfit urls count is ' + str(len(unfit_urls)))\n",
    "# print(unfit_urls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 735,
   "id": "c1c90993",
   "metadata": {},
   "outputs": [],
   "source": [
    "# fit_recs = []\n",
    "# for rec in recs[:]:\n",
    "#     link = rec.link().strip()\n",
    "#     #print(link)\n",
    "#     if link in unfit_urls:\n",
    "#         print('unfit url is ' + rec.link())\n",
    "#     else:\n",
    "#         fit_recs.append(rec)\n",
    "# print('Fit records count is ' + str(len(fit_recs)))\n",
    "# with open(output_fit_file, 'w', encoding='utf-8') as file:\n",
    "#     for rec in fit_recs:\n",
    "#         file.write(rec.text() + '\\n')\n",
    "#         file.write('\\t链接\\n')\n",
    "#         file.write(rec.link() + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 736,
   "id": "3014347e",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Left records count is 100\n",
      "Matched records count is 17\n",
      "1.实现strstr()KMP字符匹配\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353157761145970688?sourceSSR=search\n",
      "逻辑题：一个坐标系放一百万个长方形，怎么存放，给你一个点，怎么判断这个点在多少个长方形边上或者内部\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/399897927387295744?sourceSSR=search\n",
      "圆形棋盘下棋\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353157692090949632?sourceSSR=search\n",
      "4.跳台阶\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/397415131720822784?sourceSSR=search\n",
      "智力题64匹马，8个赛道，比出前四名\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/405049196586319872?sourceSSR=search\n",
      "7.给定一个矩形和圆，判断是否相交\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353154523936006144?sourceSSR=search\n",
      "一个两数之和\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/feed/main/detail/67c8458e522643e7a54ba03524b43808?sourceSSR=search\n",
      "怎么确定一个点在多边形内部\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/410554478237065216?sourceSSR=search\n",
      "1.给出n个点（x,y) n个点两两组合成一条直线，求这些直线斜率第k大的斜率，1<=n<=100000。    \n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353156856933720064?sourceSSR=search\n",
      " 1.给出n个点（x,y) n个点两两组合成一条直线，求这些直线斜率第k大的斜率，1<=n<=100000。    \n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353156856933720064?sourceSSR=search\n",
      "2.一道古典概率\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353156251649515520?sourceSSR=search\n",
      "1. 分发糖果\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353159556740096000?sourceSSR=search\n",
      "第一道青蛙爬井，ac\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353158365780058112?sourceSSR=search\n",
      "最后判断窗口数和k的大小\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/601121344389980160?sourceSSR=search\n",
      "三数之和\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353157880146763776?sourceSSR=search\n",
      "组合出算式等于N。\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/discuss/353155486629437440?sourceSSR=search\n",
      "1. 64匹马8个赛道选出前五\n",
      "\t链接\n",
      "\t\thttps://www.nowcoder.com/feed/main/detail/e93c93a39e7946f196012ef370b9d3fd?sourceSSR=search\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "match_recs = []\n",
    "left_recs = []\n",
    "for rec in recs[:]:  # 使用original_list的副本进行迭代，避免修改过程中迭代器失效\n",
    "    match = False\n",
    "    if use_re_match:\n",
    "        for key_word in key_words:\n",
    "            if re.match(key_word, rec.text()):\n",
    "                match = True\n",
    "                break\n",
    "            #new_list.append(num)\n",
    "            #original_list.remove(num)\n",
    "    else:\n",
    "        for key_word in key_words:\n",
    "            key_word_lower = key_word.lower()\n",
    "            if key_word_lower in rec.text().lower():\n",
    "                match = True\n",
    "                break\n",
    "    if match:\n",
    "        match_recs.append(rec)\n",
    "    else:\n",
    "        left_recs.append(rec)\n",
    "\n",
    "print('Left records count is ' + str(len(left_recs)))\n",
    "print('Matched records count is ' + str(len(match_recs)))\n",
    "for rec in match_recs:\n",
    "    print(rec.text())\n",
    "    print('\\t链接')\n",
    "    print(rec.link())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 737,
   "id": "3af1132b",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(output_match_file, 'w', encoding='utf-8') as file:\n",
    "    for rec in match_recs:\n",
    "        file.write(rec.text() + '\\n')\n",
    "        file.write('\\t链接\\n')\n",
    "        file.write(rec.link() + '\\n')\n",
    "        \n",
    "with open(output_left_file, 'w', encoding='utf-8') as file:\n",
    "    for rec in left_recs:\n",
    "        file.write(rec.text() + '\\n')\n",
    "        file.write('\\t链接\\n')\n",
    "        file.write(rec.link() + '\\n')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
